/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.polimi.deib.se2.meteocal.gui;

import it.polimi.deib.se2.meteocal.business.boundary.EventManager;
import it.polimi.deib.se2.meteocal.business.boundary.InvitationManager;
import it.polimi.deib.se2.meteocal.business.boundary.NotificationManager;
import it.polimi.deib.se2.meteocal.business.control.Utils;
import it.polimi.deib.se2.meteocal.business.entity.Event;
import it.polimi.deib.se2.meteocal.business.entity.Invitation;
import it.polimi.deib.se2.meteocal.business.entity.Notification;
import it.polimi.deib.se2.meteocal.business.entity.User;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.joda.time.DateTime;
import org.json.JSONException;

@Named
@RequestScoped
public class NotificationBean {
    
    @EJB
            NotificationManager nm;
    @EJB
            InvitationManager im;
    @EJB
            EventManager em;
    @Inject
    private UserBean ub;
    @Inject
    private CalendarBean cb;
    
    
    public NotificationBean() {
    }
    
    public List<Notification> getNotifications(){
        User loggedUser = ub.getUser();
        return nm.getNotificationsByUser(loggedUser);
    }
    
    public boolean isUpdate (Notification notification){
        String[] splittedString = notification.getDetails().split(" ");
        String notificationType = splittedString[1];
        boolean isUpdate;
        if("rescheduled".equals(notificationType)){
            isUpdate = true;
        }else
            isUpdate = false;
        
        return isUpdate;
    }
    
    public void acceptNotification(Notification notification) throws JSONException, IOException{
        
        String[] splittedString = notification.getDetails().split(" ");
        String notificationType = splittedString[1];
        if("invitation".equals(notificationType)){
            Event e = notification.getReferingEvent();
            cb.addOtherUserEvent(e);
            
            //update invitation status from false(still pending) to true.
            Invitation relativeInvitation = notification.getInvitation();
            relativeInvitation.setStatus(true);
            im.update(relativeInvitation);
        }
        if("rescheduling".equals(notificationType)){
            Event referingEvent = notification.getReferingEvent();
            Date startDate = referingEvent.getStartDate();
            int diff = Utils.getDateDiff(startDate, TimeUnit.DAYS);
            DateTime startDT = new DateTime(startDate);
            Date newStartDate = startDT.plusDays(diff).toDate();
            
            Date endDate = referingEvent.getEndDate();
            DateTime endDT = new DateTime(endDate);
            Date newEndDate = endDT.plusDays(diff).toDate();
            
            cb.modifyEvent(referingEvent, newStartDate, newEndDate);
        }
        
        
        deleteNotification(notification, notificationType);
    }
    
    public void declineNotification(Notification notification) throws JSONException, IOException{
        String[] splittedString = notification.getDetails().split(" ");
        String notificationType = splittedString[1];
        
        deleteNotification(notification, notificationType);
    }
    
    private void deleteNotification(Notification notification, String notificationType) throws JSONException, IOException{
        
        //L'EVENTO NON POSSIEDE NOTIFICATION
        //rimuovere dalla lista di notifiche dell'event
        notification.getReferingEvent().getNotifications().remove(notification);//se non lo rimuove forse devo ridefinire la equals all'interno di Notification
        
        
        //rimuovere dalla lista di notifiche dell'user
        notification.getUserPinged().getNotifications().remove(notification);
        
        
        //rimuovere da invitation a poi fare il merge perché è nel DB
        if("invitation".equals(notificationType)){
            Invitation relativeInvitation = notification.getInvitation();
            relativeInvitation.setNotification_id(null);
            im.update(relativeInvitation);
        }
        //rimuovere notification dal DB
        nm.remove(notification);
    }
    
}
